/*
 * @Author       : borfish
 * @Date         : 2025-08-07 16:39:55
 * @LastEditors  : borfish
 * @LastEditTime : 2025-08-12 11:17:08
 * @FilePath     : /include/upperMain.h
 * @Description  : 
 * Copyright 2025 OBKoro1, All Rights Reserved. 
 * 2025-08-07 16:39:55
 */
#pragma once
#include <QMainWindow>
#include <qmqttclient.h>

#include <QTabWidget>
#include <QTextEdit>
#include <QLineEdit>
#include <QPushButton>
#include <QComboBox>
#include <QLabel>
#include <QStatusBar>
#include <QGroupBox>
#include <QSplitter>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QStyleFactory>
#include <QFile>
#include <QSvgRenderer>
#include <QPainter>

#include "include/MqttHandler.h"

class IconButton : public QPushButton {
public:
    IconButton(const QString &iconPath,QWidget *parent = nullptr)
        : QPushButton(parent), m_iconPath_(iconPath) {
            setFlat(true);
            setFixedSize(32, 32);
        }
protected:
    void paintEvent(QPaintEvent *event) override {
        Q_UNUSED(event);
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        QSvgRenderer renderer(m_iconPath_);
        if (!renderer.isValid()) {
            qDebug() << "Invalid SVG file:" << m_iconPath_;
            return;
        }

        // 获取SVG原始尺寸
    QSize svgSize = renderer.defaultSize();
    if (svgSize.isEmpty()) {
        // 如果SVG没有默认尺寸，使用按钮尺寸
        renderer.render(&painter, rect());
        return;
    }

        // 计算缩放比例，保持SVG原始宽高比
        qreal scale = qMin((qreal)width() / svgSize.width(), (qreal)height() / svgSize.height());
        int scaledWidth = svgSize.width() * scale;
        int scaledHeight = svgSize.height() * scale;

        // 计算居中位置
        int x = (width() - scaledWidth) / 2;
        int y = (height() - scaledHeight) / 2;

        // 渲染缩放后的SVG（居中显示，无拉伸）
        renderer.render(&painter, QRect(x, y, scaledWidth, scaledHeight));
    }
private:
    QString m_iconPath_;
};


class upperMain : public QMainWindow {
    Q_OBJECT

public:
    upperMain(QWidget* parent = nullptr);
    ~upperMain();

    void applyStyles();

private slots:
    // UI操作
    void connectClicked();      ///< 处理连接按钮点击事件
    void disconnectClicked();   ///< 处理断开连接按钮点击事件
    void publishClicked();      ///< 处理发布消息按钮点击事件
    void subscribeClicked();    ///< 处理订阅按钮点击事件
    void unsubscribeClicked();  ///< 处理取消订阅按钮点击事件
    void clearLog();            ///< 处理清空日志按钮点击事件

    // MQTT状态更新
    void onMqttConnected();     ///< 处理MQTT连接成功事件
    void onMqttDisconnected();  ///< 处理MQTT断开连接事件
    void onMqttError(const QString &error); ///< 处理MQTT错误事件

    // MQTT消息处理
    void onMessageReceived(const QString &topic,const QString &message);    ///< 处理接收到的MQTT消息

    // 日志处理
    void onLogMessage(const QString &message);  ///< 处理日志消息显示

private:
    void creatUI();             //初始化并布局所有UI控件，设置初始状态
    void updateUIState();       //根据MQTT连接状态启用或禁用相关控件
    void setupConnections();    //将UI控件的信号与对应的槽函数连接，以及MQTT管理器的信号与UI处理函数连接

    // UI组件
    QLineEdit *m_hostEdit_;
    QLineEdit *m_portEdit_;
    QLineEdit *m_usernameEdit_;
    QLineEdit *m_passwordEdit_;
    QPushButton *m_connectButton_;
    QPushButton *m_disconnectButton_;
    QLineEdit *m_topicEdit_;
    QComboBox *m_qosCombo_;              ///< QoS服务质量等级选择下拉框

    QLineEdit *m_pubTopicEdit_;          //发布消息
    QLineEdit *m_pubMessageEdit_;
    QPushButton *m_publishButton_;

    QLineEdit *m_subTopicEdit_;          //订阅消息
    QTextEdit *m_subMessageDisplay_;
    QPushButton *m_subscribeButton_;
    QPushButton *m_unsubscribeButton_;

    QTextEdit *m_logDisplay_;
    QStatusBar *m_statusBar_;            ///< 状态栏
    QLabel *m_statusLabel_;
    QLabel *m_messageCountLabel_;
    QLabel *m_errorCountLabel_;
    QPushButton *m_clearLogButton_;
    
    MQTTHandler *m_mqttHandler_;         ///< MQTT管理器实例，负责处理MQTT协议相关操作

    int m_messageCount_ = 0;
    int m_errorCount_ = 0;
};
